cd D:\北工大\B503\合作论文\注册制和金融效率\code
* step1
clear
import excel "D:\北工大\B503\合作论文\注册制和金融效率\数据\通过检验\psm_data_2019.xlsx", sheet("Sheet1") firstrow

gen policy = Time - 2020
tab policy
replace policy = -3 if policy < -3
replace policy =3 if policy > 3


* 生成年份虚拟变量与实验组虚拟变量的交互项  
forvalues i = 3(-1)1 {  
    gen pre_`i' = (policy == -`i' & treat == 1)  
}  
gen current = (policy == 0 & treat == 1)  
forvalues j = 1(1)3 {  
    gen post_`j' = (policy == `j' & treat == 1)  
}  
  

* step2 
  
* 运行reghdfe命令（这里只运行一个作为示例）  
* 注意：确保weight_in_kg, male, height, study_online, x1, x2, x3, x4, x5, x6等变量都已正确定义  
* 省份 行业级别 Time   * 省份 行业级别 * 省份 Time *省份 省份 行业级别 Time   
 
//  reghdfe y pre_3 pre_2 pre_1 current post_1 post_2 post_3 x1 x2 x3 x4 x5 x6, absorb(Time)  

//  reghdfe y pre_3 pre_2 pre_1 current post_1 post_2 post_3 x1 x2 x3 x4 x5 x6, absorb(省份)  

//  reghdfe y pre_3 pre_2 pre_1 current post_1 post_2 post_3 x1 x2 x3 x4 x5 x6, absorb(行业级别) 

//  reghdfe y pre_3 pre_2 pre_1 current post_1 post_2 post_3 x1 x2 x3 x4 x5 x6, absorb(省份 Time)  

//  reghdfe y pre_3 pre_2 pre_1 current post_1 post_2 post_3 x1 x2 x3 x4 x5 x6, absorb(行业级别 Time)  

//  reghdfe y pre_3 pre_2 pre_1 current post_1 post_2 post_3 x1 x2 x3 x4 x5 x6, absorb(省份 行业级别)  

 reghdfe y pre_3 pre_2 pre_1 current post_1 post_2 post_3 x1 x2 x3 x4 x5 x6, absorb(省份 行业级别 Time)  
 
 * 输出结果并检查pre_变量的显著性  
// outreg2 using "parallel_trend_test.doc", replace bdec(3)  tdec(3) addstat(N, R-sq) word dec(3) title("平行趋势检验的DID回归结果")  
outreg2 using "parallel_trend_test.doc", replace bdec(3)  tdec(3) word dec(3)  title("平行趋势检验的DID回归结果")

* 例如，检查pre_3, pre_2, pre_1的系数是否显著异于0，以验证平行趋势假设  
* 如果这些系数在统计上不显著，则支持平行趋势假设  
 
* 画图
coefplot, baselevels vertical keep(pre_3 pre_2  pre_1 current post_1 post_2 post_3) ///  
    omitted order(pre_3 pre_2 pre_1 current post_1 post_2 post_3) ///  
    level(95) yline(0, lcolor(edkblue*0.8)) ///  
    xline(3, lwidth(vthin) lpattern(dash) lcolor(teal)) ///  
    ylabel(, labsize(*0.75)) xlabel(, labsize(*0.75)) ///  
    ytitle("注册制动态效应", size(small)) ///  
    xtitle("注册制时点", size(small)) ///  
    addplot(line @b @at) ///  
    ciopts(lpattern(dash) recast(rcap) msize(medium)) ///  
    msymbol(circle_hollow) scheme(s1mono)